class MutableIntRef{
  private MutableIntRef ref;
  private int val;

  public MutableIntRef(){
    ref = null;
    val = -1;
  } 

  // get -------------------------------------

  public int getVal(){
    if(this.ref == null || this.ref == this){
      return this.val;
    }else{
      return this.getRef().getVal();
    }
  } // getVal()


  public MutableIntRef getRef(){
    if(this.ref != null && this.ref != this){
      return this.ref.getRef();
    }
    else{
      return this;
    } // if ref != null
  } // getRef()

  // set ----------------------------------

  public void setVal(int val){
    if(this.ref != this){
      if(this.ref == null){
        this.val = val;
      }else{
        this.getRef().setVal(val);
      }
    }
  } // setVal()

  public void setRef(MutableIntRef ref){
    if(this.ref != this){
      if(this.ref == null){
        this.ref = ref;
      }else{
        this.getRef().setRef(ref);
      } // if this.ref == null
    } // if this.ref != this
  } // setRef()
} // MutableIntRef

